home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / native / implementing / example / Replace.java < prev   
Encoding:
Java Source  |  1997-07-13  |  2.8 KB  |  85 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. class Replace {
  18.  
  19.     public static void Usage() {
  20.         System.out.println("\nUsage:  java Replace char1 char2 inFile outFile");
  21.     }
  22.  
  23.     public static void main(String[] args) {
  24.         InputFile         in = null;
  25.        OutputFile        out = null;
  26.              char     former = 'A';
  27.              char     latter = 'A';
  28.            byte[]        buf;
  29.  
  30.         try {
  31.                former = args[0].charAt(0);
  32.         } catch (ArrayIndexOutOfBoundsException e) {
  33.             Usage();
  34.             System.err.println("you must supply the character to replace\n");
  35.             System.exit(-1);
  36.         }
  37.  
  38.         try {
  39.                latter = args[1].charAt(0);
  40.         } catch (ArrayIndexOutOfBoundsException e) {
  41.             Usage();
  42.             System.err.println("you must supply the new character\n");
  43.             System.exit(-1);
  44.         }
  45.  
  46.         try {
  47.             in = new InputFile(args[2]);
  48.         } catch (ArrayIndexOutOfBoundsException e) {
  49.             Usage();
  50.             System.err.println("you must supply the input replacement file\n");
  51.             System.exit(-1);
  52.         }
  53.  
  54.         try {
  55.             out = new OutputFile(args[3]);
  56.         } catch (ArrayIndexOutOfBoundsException e) {
  57.             Usage();
  58.             System.err.println("you must supply the output replacement file\n");
  59.             System.exit(-1);
  60.         }
  61.  
  62.         System.out.println("Replacing "+args[0]+" with "+args[1]+" from "+
  63.                            args[2]+" to "+args[3]);
  64.  
  65.         if (in.open() == false) {
  66.             System.out.println("Unable to open input file "+in.getFileName());
  67.         }
  68.  
  69.         if (out.open() == false) {
  70.             System.out.println("Unable to open output file "+out.getFileName());
  71.         }
  72.  
  73.         buf = new byte[1];
  74.         while (in.read(buf, 1) == 1) {
  75.             if (buf[0] == former)
  76.                 buf[0] = (byte)latter;
  77.             if (out.write(buf, 1) != 1) {
  78.                 System.out.println("Error writing to "+out.getFileName());
  79.             }
  80.         }
  81.          in.close();
  82.          out.close();
  83.     }
  84. }
  85.